package com.rest.demo.concurrent;

/**
 * @author Administrator
 * @decription
 * @Date 2018-02-01 10:15
 */
public class MultiShareData {

    public static void main(String[] args) {

        DataShare dataShare = new DataShare();

        for (int i = 0; i < 2; i++) {
            new Thread(() -> dataShare.increment()).start();
        }
        for (int i = 0; i < 2; i++) {
            new Thread(() -> dataShare.decrement()).start();
        }
    }
}

class DataShare {

    private int data = 0;

    public synchronized void increment() {
        System.out.println(Thread.currentThread().getName() + ": before : " + data);
        data++;
        System.out.println(Thread.currentThread().getName() + ": after : " + data);
    }

    public synchronized void decrement() {
        System.out.println(Thread.currentThread().getName() + ": before : " + data);
        data--;
        System.out.println(Thread.currentThread().getName() + ": after : " + data);
    }

}
